package Demo3;

public class Solution {
    public boolean isHappy(int n) {
        //以数字为下标，快慢指针思想，看相遇的位置是否为1
        int slow = n,fast = bitSum(n);
        while(slow != fast){
            //注意这里求平方和的对象！！！
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        }
        return slow == 1;
    }

    public int bitSum(int n){
        //返回n这个数每一个位置上的平方和
        int sum = 0;
        while(n > 0){
            int t = n % 10;
            sum += t*t;
            n /= 10;
        }
        return sum;
    }
}